import { HttpAdapterHost, NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { HttpExceptionFilter } from './filter/http-exception.filter';
import { ResultInterceptor } from './interceptor/result.interceptor';
import { ValidationPipe } from '@nestjs/common'; //新加
import { NestExpressApplication } from '@nestjs/platform-express';
import * as path from 'path';
import { WsAdapter } from '@nestjs/platform-ws';

async function bootstrap() {
  const app = await NestFactory.create<NestExpressApplication>(AppModule);
  const httpAdapter = app.get(HttpAdapterHost);
  app.enableCors();

  // // 设置websockets
  // app.useWebSocketAdapter(new WsAdapter(app));

  app.setGlobalPrefix('api/v1');
  app.useGlobalInterceptors(new ResultInterceptor());
  app.useStaticAssets(path.join(__dirname, '..', 'files'), {
    prefix: '/files',
  });

  // 要在全局加入验证管道  不然无效
  app.useGlobalPipes(new ValidationPipe());

  app.useGlobalFilters(new HttpExceptionFilter(httpAdapter));
  await app.listen(3000);
}
bootstrap();
